package im.elephas.androfan;

import java.util.List;

import im.elephas.fanfouapi.FanfouApi;
import im.elephas.fanfouapi.events.IErrorEvent;
import im.elephas.fanfouapi.events.IUsersEvent;
import im.elephas.fanfouapi.items.UserItem;
import im.elephas.util.DateFormater;
import im.elephas.util.ImageGetter;
import im.elephas.util.TagFilter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FollowersActivity extends Activity implements IUsersEvent,
		IErrorEvent {

	private FollowersActivity mFollowersActivity = this;
	private FanfouApi mFanfouApi;

	private LinearLayout linearLayout;

	private List<UserItem> followersListUserItems;
	private UserItem currentItem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statusesfreshing);

		mFanfouApi = FanfouApi.getInstance();

		setTitle(mFanfouApi.getUser().getScreenName() + " 关注我的:"
				+ mFanfouApi.getUser().getFollowersCount());

		mFanfouApi.getFollowers(mFanfouApi.getUser().getId(), this, this);
	}

	@Override
	public void errorHandler() {
		mFollowersActivity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				Builder dialog = new Builder(mFollowersActivity);
				dialog.setTitle("获取消息失败！请检查网络设置。");
				dialog.show();
			}
		});
	}

	@Override
	public void usersHandler(List<UserItem> lui) {
		followersListUserItems = lui;
		mFollowersActivity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				refreshFollowers();
			}
		});
	}

	private void refreshFollowers() {
		setContentView(R.layout.friendslist);

		linearLayout = (LinearLayout) findViewById(R.id.friendsLinearLayout);

		boolean colorDark = false;
		for (final UserItem item : followersListUserItems) {
			View view = getLayoutInflater().inflate(R.layout.friendsitem, null);

			if (colorDark)
				view.setBackgroundColor(Color.argb(255, 202, 222, 235));
			else
				view.setBackgroundColor(Color.argb(255, 237, 237, 237));
			colorDark = !colorDark;

			TextView nameTextView = (TextView) view
					.findViewById(R.id.nameTextView);
			TextView locationTextView = (TextView) view
					.findViewById(R.id.locationTextView);
			ImageView avatarImageView = (ImageView) view
					.findViewById(R.id.avatarImageView);

			nameTextView.setText(item.getScreenName());
			locationTextView.setText("所在地: " + item.getLocation());
			avatarImageView.setImageBitmap(ImageGetter.getBitMap(item
					.getProfileImageUrl()));

			linearLayout.addView(view);

			view.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {
					currentItem = item;
					showSelectTextMenuDialog();
					return true;
				}
			});
			avatarImageView.setClickable(true);
			avatarImageView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					currentItem = item;
					showSelectTextMenuDialog();
				}
			});
		}
	}

	private void showSelectTextMenuDialog() {
		Builder dialog = new AlertDialog.Builder(this);
		String[] sel = { "回复", "查看资料" };
		dialog.setTitle("对@" + currentItem.getScreenName());
		dialog.setItems(sel, new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent i;
				Bundle b;
				switch (which) {
				case 0:
					i = new Intent(mFollowersActivity,
							SendMessageActivity.class);
					b = new Bundle();
					b.putBoolean("isreply", true);
					b.putString("atname", currentItem.getName());
					b.putString("replyid", currentItem.getId());
					i.putExtras(b);
					startActivityForResult(i, 0);
					break;
				case 1:
					i = new Intent(mFollowersActivity,
							PersonalInfoActivity.class);
					b = new Bundle();
					b.putString("name", currentItem.getScreenName());
					b.putString("statuses", currentItem.getStatusesCount());
					b.putString("followers", currentItem.getFollowersCount());
					b.putString("friends", currentItem.getFriendsCount());
					b.putString("profile", currentItem.getProfileImageUrl());
					b.putString("description", currentItem.getDescription());
					b.putString("id", currentItem.getId());
					i.putExtras(b);
					startActivityForResult(i, 0);
					break;
				default:
					break;
				}
			}
		});
		dialog.show();
	}
}
